import * as path from "path";

const Koa = require('koa');
const koaBody = require('koa-body');
const KoaStatic = require('koa-static');
const os = require("os");
import api from "./router"

const port = process.env.PORT || 12021

/**
 * 获取当前机器的ip地址
 */
const getIpAddress = () => {
  const iFaces = os.networkInterfaces();
  let localAddress = '';
  Object.keys(iFaces).forEach((dev) => {
    const iFace = iFaces[dev];

    for (let i = 0; i < iFace.length; i += 1) {
      const { family, address, internal } = iFace[i];

      if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {
        localAddress = address;
      }
    }
  });
  return localAddress;
};

const ipAddress = getIpAddress();

const app = new Koa()
app.use(koaBody({
  multipart: true,
  formidable: {
    keepExtensions: true,
  },
}));
app.use(KoaStatic(path.join(__dirname,"../dist-client")))

api(app);

app.listen(port, () => {
  console.log('服务器挂着完成... ');
  console.log(`http://localhost:${port}/`);
  console.log(`http://${ipAddress}:${port}/`);
});
